﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using netcore_comm.Redis;
using netcore_comm.SqlSugar;
using netcore_service.Dto;
using netcore_service.IRepository;
using netcore_service.IService;
using netcore_service.Model;
using Polly;
using Polly.RateLimit;
using System.Threading;

namespace netcore_service.Service
{

    public class AdSpaceService : IAdSpaceService
    {
        private readonly IAdSpaceRepository _adSpaceRepository;
        private readonly ISqlSugarFactory _sqlSugarFactory;
        private readonly RedisHelper _redisHelper;
        public AdSpaceService(IAdSpaceRepository adSpaceRepository, ISqlSugarFactory sqlSugarFactory, RedisHelper redisHelper)
        {
            _sqlSugarFactory = sqlSugarFactory;
            _adSpaceRepository = adSpaceRepository;
            _redisHelper = redisHelper;
        }

        [HttpGet]
        [Authorize]
        public async Task<ApiResult<AdSpace>> AdSpaceByIdAsync(int id)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            return APIResult.GetSuccess<AdSpace>(await _adSpaceRepository.GetModelAsync(db, e => e.Id == id));
        }

        [HttpGet]
        public async Task<ApiResult<List<AdSpaceDto>>> GetAdSpaceByTypeAsync(string type, bool isweixin = false)
        {
            if (!string.IsNullOrEmpty(type))
            {
                if (type != "img" && type != "vedio")
                {
                    return APIResult.GetError<List<AdSpaceDto>>("参数错误");
                }
            }
            var model = await _redisHelper.HashGetAsync<List<AdSpaceDto>>(type, isweixin.ToString());
            if (model == null)
            {
                model = await _adSpaceRepository.GetAdSpaceByTypeAsync(type, isweixin);
                if (model != null)
                {
                    await _redisHelper.HashSetAsync<List<AdSpaceDto>>(type, isweixin.ToString(), model);
                }
            }

            return APIResult.GetSuccess<List<AdSpaceDto>>(model);


        }

        [Authorize]
        [HttpPost]
        public async Task<ApiResult<string>> AdSpaceDelAsync(int id)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData), DBOperateType.Write);
            if (id > 0)
            {
                await _redisHelper.KeyDeleteAsync("img");
                await _redisHelper.KeyDeleteAsync("vedio");
                await _adSpaceRepository.DeleteAsync(db, new AdSpace() { Id = id });
            }
            return APIResult.GetSuccess("删除成功");
        }

        [HttpGet]
        public async Task<ApiResult<List<AdSpace>>> AdSpaceList(bool isweixin = false)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            return APIResult.GetSuccess<List<AdSpace>>(await _adSpaceRepository.GetListAsync(db, e => e.IsActive && e.IsWeixin == isweixin));
        }

        [Authorize]
        [HttpPost]
        public async Task<ApiResult<List<AdSpace>>> AdSpaceListAsync()
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            return APIResult.GetSuccess<List<AdSpace>>(await _adSpaceRepository.GetListAsync(db));
        }

        [Authorize]
        [HttpPost]
        public async Task<ApiResult<string>> AdSpaceUpdateOrAddAsync(AdSpace adSpace)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData), DBOperateType.Write);
            if (adSpace != null)
            {
                await _redisHelper.KeyDeleteAsync("img");
                await _redisHelper.KeyDeleteAsync("vedio");
                if (adSpace.Id == 0)
                {
                    //添加
                    adSpace.ModifyUser = "admin";
                    var s = await _adSpaceRepository.InsertAsync(db, adSpace);
                }
                else
                {
                    //修改
                    await _adSpaceRepository.UpdateAsync(db, adSpace);
                }
            }
            else
            {
                return APIResult.GetError("实体为空！");
            }
            return APIResult.GetSuccess("操作成功");
        }
    }
}
